package com.tongyi.lizhishop.activitys;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.StoreApi;
import com.tongyi.lizhishop.beans.RQCodeBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.common.MultiStatusActivity;
import com.tongyi.lizhishop.managers.APIServiceManager;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CollectionMoneyActivity extends MultiStatusActivity implements View.OnClickListener {

    TextView time;
    TextView money;
    TextView order;
    Button btn;
    private TextView rightTextView;
    private AlertDialog.Builder builder;
    private AlertDialog dialog;
    private ImageView qrcodeimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "收款");
        titlebar.setBackgroundColor(getResources().getColor(R.color.mainColor));
        titlebar.getCenterTextView().setTextColor(Color.WHITE);
        titlebar.getLeftTextView().setTextColor(Color.WHITE);
        titlebar.hiddenBottomLine();
        rightTextView = titlebar.getRightTextView();
        rightTextView.setTextColor(Color.WHITE);
        rightTextView.setText("说明");
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        qrcodeimage = (ImageView) findViewById(R.id.qrcodeimage);
        loadData();
    }

    private void loadData() {

        APIServiceManager.getInstance().create(StoreApi.class)
                .showImage(SPUtils.getInstance().getString("userid"))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<RQCodeBean>() {
                    @Override
                    public void onSuccess(RQCodeBean value) {
                        String url = value.getUrl();
                        if (TextUtils.isEmpty(value.getUrl())) {
                            ToastUtils.showShort("服务器返回的数据不正确");
                        } else {
                            Glide.with(CollectionMoneyActivity.this)
                                    .load(APIServiceManager.getInstance().baseUrl() + url)
                                    .into(qrcodeimage);
                        }
                        LogUtils.w(value);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        ToastUtils.showShort("无法连接网络");
                    }
                });

    }

    @Override
    public int getContentView() {
        return R.layout.activity_collection_money;
    }

    public void showDialog() {

        if (1 == 1) {
            Bundle bundle = new Bundle();
            bundle.putString("titlebar", "使用说明");
            bundle.putString("url", "/frontend.php/Img/yueshiyongshuoming");
            ActivityUtils.startActivity(bundle, this, PlatformIntroduceActivity.class);
            return ;
        }

        View view = getLayoutInflater().inflate(R.layout.dialog, null);
        time = (TextView) view.findViewById(R.id.time);
        money = (TextView) view.findViewById(R.id.money);
        order = (TextView) view.findViewById(R.id.order);

        btn = (Button) view.findViewById(R.id.btn);
        btn.setOnClickListener(this);
        builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
                .setView(view)
                .setCancelable(false);
        dialog = builder.create();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
// 设置宽高为match_parent，不要去算出来屏幕宽高再赋值哦，因为有些
// 有虚拟按键的手机上计算出来的高度不一定准确，所以dialog不会全屏
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(params);
        dialog.show();
        // style需要自己写，只用消除title属性就够了，也不用设置parent
/*        Dialog dialog = new Dialog(this,R.style.dialog_fullscreen);
        dialog.setContentView(R.layout.dialog);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
// 设置宽高为match_parent，不要去算出来屏幕宽高再赋值哦，因为有些
// 有虚拟按键的手机上计算出来的高度不一定准确，所以dialog不会全屏
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(params);
// 设置dialog距屏幕的边距都为0
        dialog.show();*/
    }


    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
}
